import { _decorator, Component, Node, Label, director } from "cc";
import { G } from "../G";
import { GameRoot } from "../shared/GameRoot";

const { ccclass, property } = _decorator;

@ccclass
export class LoadingScene extends Component {

    @property(Label)
    private tipLabel: Label;

    @property(Node)
    private gameRoot: Node;

    // start前调用
    onLoad() {
        // 初始化全局
        G.gameRoot = this.gameRoot.getComponent(GameRoot);
        // 设置常驻节点
        director.addPersistRootNode(this.gameRoot);
    }

    // loading动画
    start() {
        let tip = "小小怪哈哈米..."
        let i = 0;
        this.tipLabel.string = '';
        this.schedule(() => {
            i++;
            if (i === tip.length + 1) {
                this.onLoadSuccess();
            } else {
                this.tipLabel.string = tip.substring(0, i);
            }
        }, 0.3, tip.length + 1, 0.3);
    }

    // start后调用
    onLoadSuccess() {
        G.enterHall();
    }
}
